﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for ORG_TaskMappingMaster.xaml
    /// </summary>
    public partial class ORG_TaskMappingMaster : Window
    {
        public static string Mode = "";
        public static string frselectionChngd = "start";
        public ORG_TaskMappingMaster()
        {
            InitializeComponent();
            InitilizeFormData();

        }
        private void InitilizeFormData()
        {
            GetTaskMappingIdentity();
            DataTable mydtSec = ORGTaskMappingMasterDAL.GetDBDetails(6);

            if (mydtSec.Rows.Count > 0)
            {
                cmbMInputSec.DataContext = mydtSec.DefaultView;
                cmbMInputSec.DisplayMemberPath = mydtSec.Columns["SectionName"].ToString();
                cmbMInputSec.SelectedValuePath = mydtSec.Columns["SectionId"].ToString();
            }
            cmbMInputEmpType.Items.Add("Select");
            cmbMInputEmpType.Items.Add("Permanent");
            cmbMInputEmpType.Items.Add("Contract");
            cmbMInputEmpType.SelectedIndex = 0;
            
            dpInputMStartDate.SelectedDate = DateTime.Now;           
            dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);

            chkInputAutoRenew.IsChecked = true;
            chkMIsActive.IsChecked = true;            
        }

        public void GetTaskMappingIdentity()
        {
            DataTable mydtTskMapID = ORGTaskMappingMasterDAL.GetDBDetails(7);
            if (mydtTskMapID.Rows.Count > 0)
            {
                if (mydtTskMapID.Rows[0][0].ToString() == "0" || mydtTskMapID.Rows[0][0].ToString() == "")
                {
                    lblInputMid.Content = 1.ToString();
                }
                else
                {
                    lblInputMid.Content = (Int32.Parse(mydtTskMapID.Rows[0][0].ToString()) + 1).ToString();
                }
            }
        }

        private void btnMSave_Click(object sender, RoutedEventArgs e)
        {
            if (validationSucess())
            {
                //bool checkEmp = true;
                btnMSave.Content = "S a v e";
                MessageBoxResult result = MessageBoxResult.No;
                string strRenew = "";
                int renew;
                string strStatus = "";
                int status;
                if (chkInputAutoRenew.IsChecked == true)
                {
                    strRenew = "Yes";
                    renew = 1;
                }
                else
                {
                    strRenew = "No";
                    renew = 0; 
                }
                if (chkMIsActive.IsChecked == true)
                {
                    strStatus = "Yes";
                    status = 1;
                }
                else
                {
                    strStatus = "No";
                    status = 0;
                }
                string strtDate = ( (DateTime) dpInputMStartDate.SelectedDate).ToShortDateString();
                string endDate = ( (DateTime)dpInputMEndDate.SelectedDate).ToShortDateString();
                DataTable mydtMapping = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 11);
                if (mydtMapping.Rows.Count > 0)
                {
                    if (cmbMInputSec.Text != mydtMapping.Rows[0][1].ToString() || cmbInputMtskName.Text != mydtMapping.Rows[0][2].ToString() || strtDate != (Convert.ToDateTime(mydtMapping.Rows[0][3].ToString())).ToShortDateString() || endDate != (Convert.ToDateTime(mydtMapping.Rows[0][4].ToString())).ToShortDateString() || strRenew != mydtMapping.Rows[0][5].ToString() || txtInputMRemarks.Text != mydtMapping.Rows[0][6].ToString() || strStatus != mydtMapping.Rows[0][7].ToString())
                    {
                        result = (MessageBox.Show("Do you want to Assign a new Task for " + cmbInputMEmpName.Text, "Information", MessageBoxButton.YesNo, MessageBoxImage.Information));
                       
                        if (result == MessageBoxResult.Yes)
                        {
                            DataTable mydtInsert = ORGTaskMappingMasterDAL.UpdateAndInsert(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(),  1);
                            DataTable mydtUpdate = ORGTaskMappingMasterDAL.UpdateAndInsert(Int32.Parse(((DataRowView)cmbMInputSec.Items[cmbMInputSec.SelectedIndex]).Row[0].ToString()), Int32.Parse(((DataRowView)cmbInputMtskName.Items[cmbInputMtskName.SelectedIndex]).Row[1].ToString()), ((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), Convert.ToDateTime(strtDate), Convert.ToDateTime(endDate), renew, txtInputMRemarks.Text, Convert.ToDateTime(System.DateTime.Now.ToShortDateString()), status, 3);
                            MessageBox.Show(cmbInputMEmpName.Text + "Details Updated Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        }             
                    }
                    else 
                    {
                        MessageBox.Show(cmbInputMEmpName.Text+" Details Already Saved", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    }           
                    
                }
                else
                {
                    DataTable mydtSave = ORGTaskMappingMasterDAL.InsertDetails(Int32.Parse(((DataRowView)cmbMInputSec.Items[cmbMInputSec.SelectedIndex]).Row[0].ToString()), Int32.Parse(((DataRowView)cmbInputMtskName.Items[cmbInputMtskName.SelectedIndex]).Row[1].ToString()), cmbMInputEmpType.Text, ((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), Convert.ToDateTime(strtDate), Convert.ToDateTime(endDate), renew, txtInputMRemarks.Text, status, 10);
                    MessageBox.Show(cmbInputMEmpName.Text+" Details Saved Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                btnMClear_Click(this, e);
            }
           
        }

        #region Validation
        private bool validationSucess()
        {
            bool status;

            if (cmbMInputEmpType.SelectedIndex == 0)
            {
                MessageBox.Show("Please Select Employee Type", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbMInputEmpType.Focus();
                return false;
            }
            if (cmbInputMEmpName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Employee Name", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbInputMEmpName.Focus();
                return false;
            }
            if (cmbMInputSec.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Section", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbMInputSec.Focus();
                return false;
            }
            if (cmbInputMtskName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Task Name", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbInputMtskName.Focus();
                return false;
            }
            if (cmbMInputEmpType.SelectedIndex == 0)
            {
                MessageBox.Show("Please Select Employee Type", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbMInputEmpType.Focus();
                return false;
            }
            if (cmbInputMEmpName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Employee Name", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbInputMEmpName.Focus();
                return false;
            }           
            if (DateTime.Today > dpInputMStartDate.SelectedDate)
            {
                MessageBox.Show("Please Select Start Date Greater than Current Date", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                dpInputMStartDate.Focus();
                return false;
            }
            if (dpInputMEndDate.SelectedDate <= dpInputMStartDate.SelectedDate)
            {
                MessageBox.Show("Please Select the End date Greater than Start Date", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                dpInputMEndDate.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void btnMClear_Click(object sender, RoutedEventArgs e)
        {

            GetTaskMappingIdentity();
            cmbInputMEmpName.SelectedIndex = -1;
            cmbMInputEmpType.SelectedIndex = 0;
            cmbInputMtskName.SelectedIndex = -1;
            cmbMInputSec.SelectedIndex = -1;
            cmbInputMtskName.DataContext = null;          
            cmbInputMtskName.SelectedIndex = -1;
            dpInputMStartDate.SelectedDate = DateTime.Now;
            dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);
            chkInputAutoRenew.IsChecked = true;
            txtInputMRemarks.Text = "";
            chkMIsActive.IsChecked = true;
            grdDisp.ItemsSource = null;
        }    

        private void cmbMInputEmpType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataTable mydtEmpType = ORGTaskMappingMasterDAL.GetDBDetails(cmbMInputEmpType.SelectedValue.ToString(), 9);
            cmbInputMEmpName.DataContext = mydtEmpType.DefaultView;
            cmbInputMEmpName.DisplayMemberPath = mydtEmpType.Columns["EName"].ToString();
            cmbInputMEmpName.SelectedValuePath = mydtEmpType.Columns["EId"].ToString();
        }
        private void cmbInputMEmpName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbInputMEmpName.SelectedIndex == -1)
            {
                return;
            }
            DataTable mydtMapping = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 11);
            DataTable mydtMappingHistory = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingHistoryMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 2);
            if (mydtMappingHistory.Rows.Count > 0)
            {
                grdDisp.ItemsSource = mydtMappingHistory;
            }
            if (mydtMapping.Rows.Count > 0)
            {
                cmbMInputSec.Text = mydtMapping.Rows[0][1].ToString();
                cmbInputMtskName.Text = mydtMapping.Rows[0][2].ToString();
                dpInputMStartDate.SelectedDate = Convert.ToDateTime(mydtMapping.Rows[0][3].ToString());
                dpInputMEndDate.SelectedDate = Convert.ToDateTime(mydtMapping.Rows[0][4].ToString());
                if (mydtMapping.Rows[0][5].ToString() == "Yes")
                {
                    chkInputAutoRenew.IsChecked = true;
                }
                else
                {
                    chkInputAutoRenew.IsChecked = false;
                }
                txtInputMRemarks.Text = mydtMapping.Rows[0][6].ToString();

                if (mydtMapping.Rows[0][7].ToString() == "Yes")
                {
                    chkMIsActive.IsChecked = true;
                }
                else
                {
                    chkMIsActive.IsChecked = false;
                }
            }
            else
            {
                cmbInputMtskName.SelectedIndex = -1;
                cmbMInputSec.SelectedIndex = -1;
                cmbInputMtskName.SelectedIndex = -1;
                dpInputMStartDate.SelectedDate = DateTime.Now;
                dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);
                chkInputAutoRenew.IsChecked = true;
                txtInputMRemarks.Text = "";
                chkMIsActive.IsChecked = true;
            }
        }
        private void dpInputMStartDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dpInputMEndDate.SelectedDate = ((DateTime)dpInputMStartDate.SelectedDate).AddMonths(1);
        }

        private void cmbMInputSec_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            if (cmbMInputSec.SelectedIndex != -1)
            {
                DataTable mydtTskNm = ORGTaskMappingMasterDAL.GetDBDetails(8);
                cmbInputMtskName.DataContext = mydtTskNm;
                cmbInputMtskName.DisplayMemberPath = mydtTskNm.Columns["TaskName"].ToString();
                cmbInputMtskName.SelectedValuePath = mydtTskNm.Columns["TaskID"].ToString();
            }
        }
    }
}
